package com.project.service.impl;

import com.project.bean.ClassBean;
import com.project.bean.CutPageBean;
import com.project.bean.StudentBean;
import com.project.mapper.IClassMapper;
import com.project.mapper.IStudentMapper;
import com.project.service.IClassService;
import com.project.service.IStudentService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ClassServiceImpl extends BaseService implements IClassService {

    @Override
    public List<ClassBean> findAll() {
        SqlSession session = this.getSession();
        IClassMapper mapper = session.getMapper(IClassMapper.class);
        List<ClassBean> list = mapper.findAll();
        session.close();
        return list;
    }


    @Override
    public boolean add(ClassBean clazz) {
        SqlSession session = this.getSession();
        IClassMapper mapper = session.getMapper(IClassMapper.class);
        if (mapper.getByName(clazz.getName()) != null) {
            session.close();
            return false;
        }
        mapper.add(clazz);
        session.commit();
        session.close();
        return true;

    }

    @Override
    public boolean del(Integer classId) {
        SqlSession session = this.getSession();
        IClassMapper mapper = session.getMapper(IClassMapper.class);
        if (mapper.getStudentNumByClassId(classId) > 0) {
            session.close();
            return false;
        }
        mapper.del(classId);
        session.commit();
        session.close();
        return true;
    }
}
